package org.nf.ssm.web.controller.admin;

import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.nf.ssm.common.ResultVO;
import org.nf.ssm.domain.Admin;
import org.nf.ssm.service.AdminService;
import org.nf.ssm.web.controller.BaseController;
import org.nf.ssm.web.vo.AdminVO;
import org.nf.ssm.web.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhu
 * @date 2024-10-14
 */
@RestController
@RequiredArgsConstructor
public class AdminController extends BaseController {

    private final AdminService adminService;

    @PostMapping("/admin/login")
    public ResultVO adminLogin(@Valid AdminVO adminVO, HttpSession session) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminVO, admin);
        Admin auth = adminService.auth(admin, session);
        return success(auth);
    }

    @GetMapping("/admin/getAllAdmin")
    public PageVO getAllAdmin(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize) {
        PageVO pageVO = new PageVO();
        PageInfo<Admin> adminPageInfo = adminService.getAllAdmin(pageNum, pageSize);
        pageVO.setTotal(adminPageInfo.getTotal());
        pageVO.setPageNum(adminPageInfo.getPageNum());
        pageVO.setPageSize(adminPageInfo.getPageSize());
        pageVO.setCode(200);
        pageVO.setData(adminPageInfo);
        return pageVO;
    }

    @PostMapping("/admin/addAdmin")
    public ResultVO addAdmin(@RequestBody Admin admin) {
        adminService.addAdmin(admin);
        return success();
    }

    @PostMapping("/admin/updateAdmin")
    public ResultVO updateAdmin(@RequestBody Admin admin) {
        adminService.updateAdmin(admin);
        return success();
    }

    @PostMapping("/admin/deleteAdmin")
    public ResultVO addAdmin(Integer adminId) {
        adminService.deleteAdmin(adminId);
        return success();
    }
}
